package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo9 {
    public static void main(String[] args) {
        try (Connection con = DBUtil.getConnection()) {
            con.setAutoCommit(false);//自动提交  如果需要自己控制提交，则设置为false
            Statement stmt = con.createStatement();
            String sql = "INSERT INTO userinfo(username,password,nickname,age) "+
                         "VALUES ('李四','123','老四',28)";
            stmt.executeUpdate(sql);
            System.out.println("添加数据成功");
            ResultSet rs = stmt.executeQuery("SELECT username FROM userinfo");
            while (rs.next()){
                System.out.println(rs.getString("username"));
            }
            con.commit();//提交  之后会开启新的事务，下次使用commit时会提交本次commit之后的所有操作提交
            //con.rollback();//回滚  回滚到上次commit的位置
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
